package cm.server.dao;

import cm.server.entity.SensorLog;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;

import java.util.List;

/**
 * @Author: rainforcetime
 * @Date: 2021/4/8 20:07
 */
public interface SensorLogRepository extends JpaRepository<SensorLog,Long> {
    @Query(value = "select * from sensor_log where time>?1 and time<?2 and day=?3", nativeQuery = true)
    List<SensorLog> findAllByPeriod(int startTime, int endTime, int day);

    @Query(value = "select * from sensor_log where user_id=?1 and day=?2 order by time", nativeQuery = true)
    List<SensorLog> findAllById(int id, int day);

    @Query(value = "select * from sensor_log \n" +
            "where (user_id, time) in (select user_id, max(time) from sensor_log where time>=?1" +
            " and time<=?2 and day=?3 group by user_id);",
            nativeQuery = true)
    List<SensorLog> findAllByLimitTime(int startTime, int endTime, int day);
}
